/**
 * @file lv_port_disp_templ.c
 *
 */

/*Copy this file as "lv_port_disp.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_disp_template.h"
#include <stdbool.h>
#include "lcd.h"
/*********************
 *      DEFINES
 *********************/
#ifndef MY_DISP_HOR_RES
#warning Please define or replace the macro MY_DISP_HOR_RES with the actual screen width, default value 320 is used for now.
#define MY_DISP_HOR_RES    LCD_W
#endif

#ifndef MY_DISP_VER_RES
#warning Please define or replace the macro MY_DISP_VER_RES with the actual screen height, default value 240 is used for now.
#define MY_DISP_VER_RES    LCD_H
#endif

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void disp_init(void);

static void disp_flush(lv_display_t *disp, const lv_area_t *area,
		lv_color16_t *color_p);
static void disp_flush888(lv_display_t *disp, const lv_area_t *area,
		lv_color_t *color_p);
/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_disp_init(void) {
	/*-------------------------
	 * Initialize your display
	 * -----------------------*/
	disp_init();

	/*------------------------------------
	 * Create a display and set a flush_cb
	 * -----------------------------------*/
	lv_display_t *disp = lv_display_create(MY_DISP_HOR_RES, MY_DISP_VER_RES);
	lv_display_set_flush_cb(disp, disp_flush888);

	/* Example 1
	 * One buffer for partial rendering*/
//    static lv_color_t buf_1_1[MY_DISP_HOR_RES * 10];                          /*A buffer for 10 rows*/
//    static lv_color_t buf_1_1[MY_DISP_HOR_RES * 10];                          /*A buffer for 10 rows*/
//    lv_display_set_buffers(disp, buf_1_1, NULL, sizeof(buf_1_1), LV_DISPLAY_RENDER_MODE_PARTIAL);
	/* Example 2
	 * Two buffers for partial rendering
	 * In flush_cb DMA or similar hardware should be used to update the display in the background.*/
	static lv_color_t buf_2_1[MY_DISP_HOR_RES * 10];
	static lv_color_t buf_2_2[MY_DISP_HOR_RES * 10];
	lv_display_set_buffers(disp, buf_2_1, buf_2_2, sizeof(buf_2_1),
			LV_DISPLAY_RENDER_MODE_PARTIAL);

	/* Example 3
	 * Two buffers screen sized buffer for double buffering.
	 * Both LV_DISPLAY_RENDER_MODE_DIRECT and LV_DISPLAY_RENDER_MODE_FULL works, see their comments*/
//    static lv_color_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES];
//    static lv_color_t buf_3_2[MY_DISP_HOR_RES * MY_DISP_VER_RES];
//    lv_display_set_buffers(disp, buf_3_1, buf_3_2, sizeof(buf_3_1), LV_DISPLAY_RENDER_MODE_DIRECT);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*Initialize your display and the required peripherals.*/
static void disp_init(void) {
	/*You code here*/
	LCD_Init();
//	LCD_Clear(BACK_COLOR);
}

volatile bool disp_flush_enabled = true;

/* Enable updating the screen (the flushing process) when disp_flush() is called by LVGL
 */
void disp_enable_update(void) {
	disp_flush_enabled = true;
}

/* Disable updating the screen (the flushing process) when disp_flush() is called by LVGL
 */
void disp_disable_update(void) {
	disp_flush_enabled = false;
}

/*Flush the content of the internal buffer the specific area on the display.
 *`px_map` contains the rendered image as raw pixel map and it should be copied to `area` on the display.
 *You can use DMA or any hardware acceleration to do this operation in the background but
 *'lv_display_flush_ready()' has to be called when it's finished.*/

static void disp_flush888(lv_display_t *disp_drv, const lv_area_t *area,
		lv_color_t *color_p) {

    if(disp_flush_enabled) {
    	int32_t x;
		int32_t y;
		for(y = area->y1; y <= area->y2; y++) {
			for(x = area->x1; x <= area->x2; x++) {
				/*Put a pixel to the display. For example:*/
				/*put_px(x, y, *px_map)*/
				uint32_t color_full = (color_p->red << 16) | (color_p->green << 8) | (color_p->blue) ;
				LCD_DrawPoint888(x, y, color_full);
				color_p++;
			}
		}
    }

//	if (disp_flush_enabled) {
//		int32_t x;
//		int32_t y;
//		int32_t sx = area->x1;
//		int32_t ex = area->x2;
//		int32_t sy = area->y1;
//		int32_t ey = area->y2;
//		uint16_t width = ex - sx + 1;
//		uint16_t height = ey - sy + 1;
//		uint32_t pixelNum = width*height;
//		uint8_t *SPI_TX_BUFF = pvPortMalloc(pixelNum * 3);
//		uint32_t num = 0;
//		for (y = area->y1; y <= area->y2; y++) {
//			for (x = area->x1; x <= area->x2; x++) {
//				/*Put a pixel to the display. For example:*/
//				/*put_px(x, y, *px_map)*/
////	        	uint32_t color_full = (color_p->red << 16) | (color_p->green << 8) | (color_p->blue) ;
//				//            	LCD_DrawPoint888(x, y, color_full);
//				*SPI_TX_BUFF = color_p->red;	//RED
//				SPI_TX_BUFF++;
//				*SPI_TX_BUFF = color_p->green;	//GREEN
//				SPI_TX_BUFF++;
//				*SPI_TX_BUFF = color_p->blue;	//BLUE
//				SPI_TX_BUFF++;
//				color_p++;
//			}
//		LCD_DrawPoint888DMA(sx, sy, ex, ey, SPI_TX_BUFF);
//		}
//		osDelay(100);
//		vPortFree(SPI_TX_BUFF);
//	}
	lv_display_flush_ready(disp_drv);

}
//static void disp_flush(lv_display_t * disp_drv, const lv_area_t * area, lv_color16_t * color_p)
//{
//    if(disp_flush_enabled) {
//
////        int32_t x;
////        int32_t y;
////		int32_t x1 = area->x1;
////		int32_t x2 = area->x2;
////		int32_t y1 = area->y1;
////		int32_t y2 = area->y2;
////		int32_t size = (x2-x1)*(y2-y1);
////		uint16_t row = (x2-x1);
////		uint16_t col = (y2-y1);
////		int **array2D = (int **)pvPortMalloc(row * sizeof(uint16_t));
////		for (int i = 0; i < row; i++) {
////		    array2D[i] = (int *)pvPortMalloc(col * sizeof(uint16_t));
////		}
////
////		for(x = area->x1; x <= area->x2; x++) {
////			for(y = area->y1; y <= area->y2; y++) {
////				uint16_t color_full = (color_p->red << 11) | (color_p->green << 5) | (color_p->blue);
////				array2D[x][y] = color_full;
////				color_p++;
////            }
////        }
////        for(y = area->y1; y <= area->y2; y++) {
////            for(x = area->x1; x <= area->x2; x++) {
////				LCD_DrawPoint(x, y, array2D[x][y]);
//////            	buff[x][y] = color_full;
////				color_p++;
////            }
////        }
////        for (int i = 0; i < row; i++) {
////            vPortFree(array2D[i]);
////        }
////        vPortFree(array2D);
////    	int32_t x1 = area->x1;
////    	int32_t x2 = area->x2;
////    	int32_t y1 = area->y1;
////    	int32_t y2 = area->y2;
////    	int32_t size = (x2-x1)*(y2-y1);
////    	uint16_t * buff = pvPortMalloc(size*2) ;
////    	for (int i = 0;  i < size; i++) {
////    		lv_color16_t * cp = color_p+i;
////    		uint16_t color_full = (cp->red << 11) | (cp->green << 5) | (cp->blue);
////    		*(buff+i) = color_full;
////    		LCD_DrawPoint(*(buff+i));
////		}
////    	Gui_DrawAreaByRam(x1, y1,x2, y2,buff,size);
//    }
//
//    /*IMPORTANT!!!
//     *Inform the graphics library that you are ready with the flushing*/
//    lv_display_flush_ready(disp_drv);
//}

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif
